package com.meritit.portal.operlog.service;

import java.sql.Connection;
import java.sql.Statement;
import java.util.UUID;

import org.apache.log4j.Logger;

import com.meritit.portal.operlog.bean.TPOperlog;
import com.meritit.portal.util.DBConnection;

/**
 * 公司: 西安美林电子有限责任公司 作者：baixw，Oct 24, 2011 描述： 说明:
 */
public class OperlogImpl implements OperlogIntfs {
    private Logger log = Logger.getLogger(OperlogImpl.class);

    public void saveOperlog(TPOperlog tplog) throws Exception {
        if (tplog == null) {
            log.error("存储用户操作日志失败。日志对象为空!");
        } else {
            Connection con = DBConnection.getConn();
            try {
                UUID uuid = UUID.randomUUID();
                String id = uuid.toString().replace("-", "").trim();
                if (id.length() > 32) {
                    id = id.substring(0, 32);
                }
                String sql = "insert into t_p_operlog(id,operdate_d,"
                        + "opermanid,stationname_v,module_v,function_v,"
                        + "operation_v,data_v,hasexception_v,exceptiontypename_v)"
                        + "values('" + id + "',to_date('"
                        + tplog.getOperdateD() + "','yyyy-mm-dd hh24:mi:ss'),'"
                        + tplog.getUsername() + "','" + tplog.getStationnameV()
                        + "','" + tplog.getModuleV() + "','"
                        + tplog.getFunctionV() + "','" + tplog.getOperationV()
                        + "','" + tplog.getDataV() + "','"
                        + tplog.getHasexceptionV() + "','"
                        + tplog.getExceptiontypenameV() + "')";
                Statement st = con.createStatement();
                st.executeUpdate(sql);
            } catch (Exception e) {
                log.error("存储用户操作日志失败,失败原因:", e);
                throw e;
            }finally{
                DBConnection.realseCon(con);
            }
        }
    }

}
